from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect
from django.shortcuts import render

 
def UserProfile(request):
    user_profile = request.user.get_profile()
			
    return render(request,"registration/profile.html",{
      'request' : request,
      'user' : request.user,
      'user_profile' : user_profile
      })	
 
class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)
 
    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")
 
    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user
		    
def UserRegister(request):
    
    if request.method == 'POST':
        form = UserCreateForm(request.POST)
        if form.is_valid():
            new_user = form.save();
            #new_user = authenticate(username=request.POST['username'], password=request.POST['password1'])
            #login(request, new_user)
            return HttpResponseRedirect('/accounts/login/')
    else:
        form = UserCreateForm()
			
    return render(request,"registration/register.html",{
      'request' : request,
      'form' : form
      })	